package com.jack.lib.test.init

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import com.jack.lib.core.ext.MM
import com.jack.lib.core.ext.fake
import com.jack.lib.core.ext.trues
import com.jack.lib.test.R
import com.jack.lib.test.view.GlobalView
import com.lutus.android.biz.mall.interval

/**
 * author : chong.huang
 * create : 2022/2/5
 * desc   :
 */
object Floating {

    private lateinit var app: Application
    private var translationX = 50F.MM
    private var translationY = 300F.MM

    fun init(app: Application) {
        this.app = app
        app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

            }

            override fun onActivityStarted(activity: Activity) {
                activity::class.java.name.startsWith("com.jack.lib.test").fake { createGlobalView(activity) }
            }

            override fun onActivityResumed(activity: Activity) {
                activity::class.java.name.startsWith("com.jack.lib.test").fake { updateGlobalView(activity) }
            }

            override fun onActivityPaused(activity: Activity) {

            }

            override fun onActivityStopped(activity: Activity) {

            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

            }

            override fun onActivityDestroyed(activity: Activity) {

            }

        })

        //requestPermission().trues { initWindow() }

    }

    private fun requestPermission(): Boolean {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && !Settings.canDrawOverlays(app)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //8.0以上
                val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                app.startActivity(intent)
                return false
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //6.0-8.0
                val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                intent.data = Uri.parse("package:" + app.packageName)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                app.startActivity(intent)
                return false
            }
        }
        return true
    }

    private fun initWindow() {

        val windowManager = app.getSystemService(Context.WINDOW_SERVICE) as WindowManager

        val lp = WindowManager.LayoutParams().also {
            it.width = 500
            it.height = 500
            //it.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE   穿透模式, view吃不到事件
            //it.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS   突破Window边缘限制
            it.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
                it.type = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                else WindowManager.LayoutParams.TYPE_STATUS_BAR
            it.format = PixelFormat.TRANSPARENT
            it.gravity = Gravity.START or Gravity.TOP
        }
        windowManager.addView(GlobalView(app).also {
            it.onDrag = { x, y ->
                lp.x += x.toInt()
                lp.y += y.toInt()
                windowManager.updateViewLayout(it, lp)
            }
        }, lp)
    }

    private fun createGlobalView(activity: Activity) {

        val globalView = activity.findViewById<View>(R.id.global_view)
        if (globalView == null) {
            activity.addContentView(GlobalView(activity).also {
                it.translationX = translationX
                it.translationY = translationY
                it.id = R.id.global_view
                it.onDrag = { x, y ->
                    it.translationX += x.toInt()
                    it.translationY += y.toInt()
                    it.translationX = it.translationX.interval(0F, activity.window.decorView.measuredWidth.toFloat() - it.measuredWidth)
                    it.translationY = it.translationY.interval(0F, activity.window.decorView.measuredHeight.toFloat() - it.measuredHeight)
                    translationX = it.translationX
                    translationY = it.translationY
                }
            }, FrameLayout.LayoutParams(-2, -2))
        }
    }

    private fun updateGlobalView(activity: Activity) {
        activity.findViewById<View>(R.id.global_view)?.also {
            it.bringToFront()
            it.translationX = translationX
            it.translationY = translationY
        }
    }
}
